package edu.upol.paradise.compiler.types;

import org.apache.log4j.Logger;

import edu.upol.paradise.runtime.FunctionArgumentsHeader;
import edu.upol.paradise.runtime.ParadiseRuntime;

public class JavaStaticMethodWrapper extends ParadiseFunctor {
	static final Logger log = Logger.getLogger(JavaStaticMethodWrapper.class);
	
	public static interface MethodWrapper {
		public ParadiseObject run(ParadiseRuntime runtime,
				VariablePassingWrapper arguments) throws Exception;
		public String name();
		public String args();
	}
	
	public JavaStaticMethodWrapper(MethodWrapper w){
		javaMethodWrapper = w;
	}
	
	private MethodWrapper javaMethodWrapper;

	public boolean valueEquals(ParadiseObject other) {
		if (other == this) return true;
		return false;
	}

	public int atomHashId() {
		return hashCode();
	}

	@Override
	public ParadiseObject run(ParadiseRuntime runtime,
			VariablePassingWrapper arguments) throws Exception {
		return javaMethodWrapper.run(runtime, arguments);
	}

	public MethodWrapper getJavaMethodWrapper() {
		return javaMethodWrapper;
	}

	public void setJavaMethodWrapper(MethodWrapper javaMethodWrapper) {
		this.javaMethodWrapper = javaMethodWrapper;
	}
	
	@Override
	public String toString(){
		return javaMethodWrapper.name();
	}
	
	private FunctionArgumentsHeader cache;

	@Override
	public FunctionArgumentsHeader getHeader() {
		if (cache == null){
			synchronized (this){
				if (cache == null){
					cache = FunctionArgumentsHeader.parse(javaMethodWrapper.args());		
				}
			}
		}
		return cache;
	}
}
